import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue')
  },
  {
    path: '/',
    component: () => import('../components/layout/AppMain.vue'),
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue')
      },
      // 药品管理
      {
        path: 'medicine/list',
        name: 'MedicineList',
        component: () => import('../views/medicine/list.vue')
      },
      {
        path: 'medicine/category',
        name: 'MedicineCategory',
        component: () => import('../views/medicine/category.vue')
      },
      {
        path: 'medicine/stock',
        name: 'MedicineStock',
        component: () => import('../views/medicine/stock.vue')
      },
      // 采购管理
      {
        path: 'purchase/order',
        name: 'PurchaseOrder',
        component: () => import('../views/purchase/order.vue')
      },
      {
        path: 'purchase/supplier',
        name: 'Supplier',
        component: () => import('../views/purchase/supplier.vue')
      },
      // 销售管理
      {
        path: 'sales/order',
        name: 'SalesOrder',
        component: () => import('../views/sales/order.vue')
      },
      {
        path: 'sales/record',
        name: 'SalesRecord',
        component: () => import('../views/sales/record.vue')
      },
      // 系统管理
      {
        path: 'system/user',
        name: 'SystemUser',
        component: () => import('../views/system/user.vue')
      },
      {
        path: 'system/role',
        name: 'SystemRole',
        component: () => import('../views/system/role.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 临时注释掉登录验证，方便开发调试
  // const token = localStorage.getItem('token')
  // if (to.path !== '/login' && !token) {
  //   next('/login')
  // } else {
  //   next()
  // }
  
  // 直接放行所有路由
  next()
})

export default router 